/**
 *********************************************************
 ** <h1>SFMLex - Simple and Fast Multimedia Library Extensions.</h1>
 ** SFMLex Copyright (C) 2008 Neil Johnson (neil@emailus.me.uk)
 **
 ** SFML - Simple and Fast Multimedia Library
 ** Copyright (C) 2007-2008 Laurent Gomila (laurent.gom@gmail.com)
 **
 ** This software is provided 'as-is', without any express or implied warranty.
 ** In no event will the authors be held liable for any damages arising from the use of this software.
 **
 ** Permission is granted to anyone to use this software for any purpose,
 ** including commercial applications, and to alter it and redistribute it freely,
 ** subject to the following restrictions:
 **
 ** 1. The origin of this software must not be misrepresented;
 **    you must not claim that you wrote the original software.
 **    If you use this software in a product, an acknowledgment
 **    in the product documentation would be appreciated but is not required.
 **
 ** 2. Altered source versions must be plainly marked as such,
 **    and must not be misrepresented as being the original software.
 **
 ** 3. This notice may not be removed or altered from any source distribution.
 **
 ************************************************************
 */

#include <sstream>

#include "c_AnimSpriteResource.h"
#include "c_ResourceMgr.h"

namespace sfx
{


   c_AnimSpriteResource::c_AnimSpriteResource( c_ResourceMgr* ResourceMgr ) :
   c_SpriteResource( ResourceMgr ),
   m_AnimSprites(),
   m_FPS_Set( false ),
   m_NumOfFrames_Set( false ),
   m_FrameWidth_Set( false ),
   m_FrameHeight_Set( false )
   {
      std::cerr << "c_AnimSpriteResource()\n";
      
      m_Map.insert( make_pair("fps", t_SetResouceFunc( boost::bind( &c_AnimSpriteResource::setFPS, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("numframes", t_SetResouceFunc( boost::bind( &c_AnimSpriteResource::setNumFrames, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("framewidth", t_SetResouceFunc( boost::bind( &c_AnimSpriteResource::setFrameWidth, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("frameheight", t_SetResouceFunc( boost::bind( &c_AnimSpriteResource::setFrameHeight, boost::ref(*this), _1 ) ) ) );
   }

   c_AnimSpriteResource::~c_AnimSpriteResource()
   {
      std::cerr << "~c_AnimSpriteResource()\n";
   }
   
   void c_AnimSpriteResource::setFPS( const std::string& FPS )
   {
      std::stringstream ss( FPS, std::stringstream::in );

      ss >> m_FPS;

      m_FPS_Set = true;
   }

   void c_AnimSpriteResource::setNumFrames( const std::string& NumberFrames )
   {
      std::stringstream ss( NumberFrames, std::stringstream::in );

      ss >> m_NumOfFrames;

      m_NumOfFrames_Set = true;
   }

   void c_AnimSpriteResource::setFrameWidth( const std::string& FrameWidth )
   {
      std::stringstream ss( FrameWidth, std::stringstream::in );

      ss >> m_FrameWidth;

      m_FrameWidth_Set = true;
   }

   void c_AnimSpriteResource::setFrameHeight( const std::string& FrameHeight )
   {
      std::stringstream ss( FrameHeight, std::stringstream::in );
      
      ss >> m_FrameHeight;

      m_FrameHeight_Set = true;
   }  
   
   c_AnimSprite* c_AnimSpriteResource::createAnimSprite( void )
   {
      unsigned   ImageWidth( 0 );
      unsigned   ImageHeight( 0 );
      sf::Image* Image( 0 );

      
      if( m_ImageName_Set )
      {
         Image = m_ResourceMgr->getImage( m_ImageName );
         if( Image )
         {
            assert( Image != 0 );
            ImageWidth  = Image->GetWidth();
            ImageHeight = Image->GetHeight();
         }
      } 

      if( m_FrameWidth_Set == false ||
          m_FrameHeight_Set == false   )
      {
         m_FrameWidth      = ImageWidth;
         m_FrameWidth_Set  = true;
         m_FrameHeight     = ImageHeight;
         m_FrameHeight_Set = true;
         m_NumOfFrames     = 1;
         m_NumOfFrames_Set = true;
      }

      if( m_FrameWidth == 0 ||
          m_FrameHeight == 0   )
      {
         //Can't create this animation
         return (c_AnimSprite*)0;
      }
      
      if( !m_NumOfFrames_Set )
      {
         m_NumOfFrames = 1;
      }

      if( !m_FPS_Set )
      {
         m_FPS = 1;
      }

      c_AnimSprite* AnimSprite;
      AnimSprite = new c_AnimSprite( *Image,
                                     m_NumOfFrames,
                                     c_AnimSprite::d_ANIM_MODE_NONE,
                                     m_FPS,
                                     m_FrameWidth,
                                     m_FrameHeight );
      assert( AnimSprite != 0 );

      if( AnimSprite )
      {
         if( m_FlipX_Set )
         {
            AnimSprite->FlipX( m_FlipX );
         }

         if( m_FlipY_Set )
         {
            AnimSprite->FlipY( m_FlipY );
         } 
         
         if( m_Position_Set )
         {
            AnimSprite->SetPosition( m_Position );
         }
       
         if( m_Centre_Set )
         {
            AnimSprite->SetOrigin( m_Centre );
         }
         
         if( m_Colour_Set )
         {
            AnimSprite->SetColor( m_Colour );
         }

         if( m_BlendMode_Set )
         {
            AnimSprite->SetBlendMode( m_BlendMode );
         }
         
         if( m_Rotation_Set )
         {
            AnimSprite->SetRotation( m_Rotation );
         }
         
         m_AnimSprites.push_back( AnimSprite );
      }
      
      return AnimSprite;
   }
   
   bool c_AnimSpriteResource::freeAnimSprite( c_AnimSprite* DeadSprite )
   {
      std::list<c_AnimSprite*>::iterator it;

      for( it = m_AnimSprites.begin(); it != m_AnimSprites.end(); ++it )
      {
         c_AnimSprite* Sprite;
         Sprite = *it;
         if( Sprite == DeadSprite )
         {
            m_AnimSprites.erase( it );
            delete Sprite;
            return true;
         }
      }

      return false;
   }

}
